package com.shenfc.fxzj.shiro;

import com.shenfc.fxzj.constants.SuperAdminEnum;
import com.shenfc.fxzj.domain.SysMenuBtn;
import com.shenfc.fxzj.domain.SysUser;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import com.shenfc.fxzj.service.ISysMenuBtnService;
import com.shenfc.fxzj.service.ISysMenuService;
import com.shenfc.fxzj.service.ISysUserService;
import com.shenfc.fxzj.util.ArrayUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义Realm
 *
 * @author sfc
 */
public class AuthRealm extends AuthorizingRealm {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysMenuService sysMenuService;
    @Autowired
    private ISysMenuBtnService sysMenuBtnService;

    /**
     * 为当限前登录的用户授予角色和权
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String userName = (String) super.getAvailablePrincipal(principals);
        SysUser sysUser = sysUserService.getSysUserByUserName(userName);

        /*设置菜单和按钮权限*/
//        List<SysMenu> sysMenuList;
        List<SysMenuBtn> sysMenuBtnList;
        if (SuperAdminEnum.YES.getKey().equals(sysUser.getSuperAdmin())) {
//            sysMenuList = sysMenuService.getAllSysMenus();
            sysMenuBtnList = sysMenuBtnService.getAllSysMenuBtns();
        } else {
//            sysMenuList = sysMenuService.getSysMenusByUserId(sysUser.getId());
            sysMenuBtnList = sysMenuBtnService.getMenuBtnsByUserID(sysUser.getId());
        }
        List<String> permList = new ArrayList<String>();
//        if (ArrayUtils.isNotEmpty(sysMenuList)) {
//            for (SysMenu sysMenu : sysMenuList) {
//                permList.add("menu:" + sysMenu.getMenuName() );
//            }
//        }

        if (ArrayUtils.isNotEmpty(sysMenuBtnList)) {
            for (SysMenuBtn sysMenuBtn : sysMenuBtnList) {
                permList.add("btn:" + sysMenuBtn.getMenuName() + ":" + sysMenuBtn.getBtnName());
            }
        }
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        info.addRoles(roleList);
        info.addStringPermissions(permList);
        return info;
//        return null;
    }

    /**
     * 验证当前登录的用户
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String userName = (String) token.getPrincipal();
        SysUser sysUser = sysUserService.getSysUserByUserName(userName);
        if (sysUser == null) {
            return null;
        }
        AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(sysUser.getUserName(), sysUser.getPassword(), ByteSource.Util.bytes(sysUser.getUserName()), sysUser.getNickName());
        return authcInfo;
    }

}
